<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

class Input{
	function str_only($string, $numbers = true, $spaces = false) {
		if($numbers == true and $spaces == false)
			$str = preg_replace("[^a-zA-Z|0-9]", "", $string);
		elseif($numbers == true and $spaces == true)
			$str = preg_replace("[^\w|\d|\s]", "", $string);
		elseif($numbers == false and $spaces == false)
			$str = preg_replace("[^a-zA-Z]", "", $string);
		elseif($numbers == false and $spaces == true)
			$str = preg_replace("[^a-zA-Z|\s]", "");
		return $str;
	}
	
	function int_only($string) {
		return intval($string);
	}
	
	function validate_ip($ip) {
		// Try IPv4 First, as its the most used method right now
		if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
			// Oops... try v6
			if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
				return false; // Sorry...
			}
			else {
				return true;
			}
		}
		else {
			return true;
		}
	}
	
	function validate_email($string) {
		// FIXME: Flawed method
		if($result = filter_var($string, FILTER_VALIDATE_EMAIL)) {
			return $result;
		}
		else return false;
	}
	
	function strip_html($string) {
		return strip_tags($string);
	}
}

$IN = new Input;
$Smarty->assign("IN", $IN);
$Debug->writeLog("Input Processed", "Core");

foreach($_GET as $key => $value){ $_GET[$key] = $DB->Driver->escape_string($_GET[$key]); }
foreach($_POST as $key => $value){ $_POST[$key] = $DB->Driver->escape_string($_POST[$key]); }
foreach($_COOKIE as $key => $value){ $_COOKIE[$key] = $DB->Driver->escape_string($_COOKIE[$key]); }
// @since rev 02, sanitize cf_uid
if(isset($_SESSION['cf_uid']))
	$_SESSION['cf_uid'] = intval($_SESSION['cf_uid']);
// @since rev 03, sanitize IP Address
if(!isset($_SERVER['REMOTE_ADDR'])) # wtf?
	die("Could not find your IP Address...");
else {
	if(!$IN->validate_ip($_SERVER["REMOTE_ADDR"])) # Although this kind of flaws isn't really possible
		die("Could not validate your IP Address...");
}